/**
 * Definition for a binary tree node.*/
struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
  right(right) {}
  };
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        queue<TreeNode*> que;
        vector<vector<int>> ret;
        if (root == nullptr)
            return ret;
        que.push(root);
        int count = 0;
        while (!que.empty()) {
            vector<int> tmp;
            int sz = que.size();
            for (int i = 0; i < sz; i++) {
                TreeNode* cur = que.front();
                tmp.push_back(cur->val);
                if (cur->left)
                    que.push(cur->left);
                if (cur->right)
                    que.push(cur->right);

                que.pop();
            }
            count++;
            if (count % 2 == 0) {
                reverse(tmp.begin(), tmp.end());
            }
            ret.push_back(tmp);
        }
        return ret;
    }
};